package com.sanjiahantang.youjiarole.module.money.views.impl;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.common.presenters.MeAccountPresenter;
import com.sanjiahantang.youjiarole.module.common.views.MeAccountView;
import com.sanjiahantang.youjiarole.module.common.views.SelectorView;
import com.sanjiahantang.youjiarole.module.money.beans.BankCardBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 添加银行卡
 * Created by admin on 2017/10/28.
 */

public class MeAddBankListActivity extends BaseMvpActivity<MeAccountView, MeAccountPresenter> implements MeAccountView, View.OnClickListener {
    private View fl_Left;
    private TextView title, btn_besure;
    private MyListView myListView;
    private QuickAdapter<BankCardBean.Item> adapter;
    private BankCardBean.Item currentItem = null;
    private String initIndex;

    @Override
    public MeAccountPresenter initPresenter() {
        return new MeAccountPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_add_banklist);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        title = (TextView) findViewById(R.id.title);
        btn_besure = (TextView) findViewById(R.id.btn_beSure);
        myListView = (MyListView) findViewById(R.id.bank_list);

        adapter = new QuickAdapter<BankCardBean.Item>($this, R.layout.item_me_banklist) {
            @Override
            protected void convert(final BaseAdapterHelper helper, final BankCardBean.Item item) {

                helper.setText(R.id.tvBankName, item.getBankGenneralName());
                helper.setText(R.id.tvBankNo,String.format("尾号%s", Utils.getLast4(item.getBankCardNo())));
                if (TextUtils.equals("1", item.getIsDefault())) {
                    helper.setChecked(R.id.cbSelected, true);
                } else {
                    helper.setChecked(R.id.cbSelected, false);
                }
                CheckBox cbSelected = helper.getView(R.id.cbSelected);
                    if (!cbSelected.isChecked()){
                        helper.setOnClickListener(R.id.cbSelected, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.updateDefaultBankCard($this, item.getMbciId(), new OnLoadListener() {
                                    @Override
                                    public void onLoad(Object content) {
                                        onResume();
                                    }
                                });
                            }
                        });
                    }else{
                        helper.setOnClickListener(R.id.cbSelected, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onResume();
                            }
                        });
                    }
                helper.setImageResource(R.id.ivBank, R.drawable.i_bank_default);
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        currentItem = item;
                        Intent intent = new Intent();
                        intent.putExtra(KEY_PARAM, currentItem);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });



            }
        };
        myListView.setAdapter(adapter);

    }

    @Override
    public void initData() {
        title.setText("我的银行卡");


        initIndex = getIntent().getStringExtra(SelectorView.KEY_INDEX);
        btn_besure.setOnClickListener(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
        loadList();
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.btn_beSure:
                startActivity(new Intent(this, MeAddBankActivity.class));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        super.onActivityResult(requestCode, resultCode, data);
    }

    private void loadList(){
        presenter.CheckBankListInfo($this, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case ACCOUNT_TYPE_COMMEN:
                        adapter.clear();
                        adapter.addAll((List<BankCardBean.Item>) param[1]);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        });
    }

    public static void startActivity(Context context){
        context.startActivity(new Intent(context,MeAddBankListActivity.class));
    }
}
